package org.iabako.client.ui.page.business.product;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.config.page.PageConfigSearchEnum;
import org.iabako.client.config.page.PageEnum;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.DefaultSuggestHandler;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.ui.form.business.product.ProductSearchForm;
import org.iabako.client.ui.layout.field.Button;
import org.iabako.client.ui.layout.widget.datatable.DataTable;
import org.iabako.client.ui.layout.widget.stock.StockTable;
import org.iabako.client.ui.layout.widget.user.tracking.TrackingPanel;
import org.iabako.client.ui.layout.widget.validation.ValidationMessage;
import org.iabako.client.user.AuthenticateUserProvider;
import org.iabako.shared.entity.enumeration.NotificationType;
import org.iabako.shared.entity.enumeration.TrackingType;
import org.iabako.shared.exception.IabakoActionForbiddenException;
import org.iabako.shared.iservice.business.ProductServiceAsync;
import org.iabako.shared.iservice.business.StockServiceAsync;
import org.iabako.shared.iservice.generic.CRUDServiceAsync;
import org.iabako.shared.iservice.generic.SearchServiceAsync;
import org.iabako.shared.iservice.generic.SuggestServiceAsync;

/**
 * Created by jose on 17/01/15.
 */
public class StockManagement extends ProductSearch {

    interface MyUiBinder extends UiBinder<HTMLPanel, StockManagement> {
    }

    private static final MyUiBinder binder = GWT.create(MyUiBinder.class);
    protected final StockServiceAsync stockServiceAsync = StockServiceAsync.Util.getInstance();

    @UiField
    HTMLPanel searchPanel;
    @UiField
    HTMLPanel resultsPanel;
    @UiField
    Button actionButton;

    public ProductSearchForm productSearchForm;
    private StockTable stockTable;

    public StockManagement() throws IabakoActionForbiddenException {
        super(false);

        actionButton.getButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (stockTable == null || stockTable.getStockModificationsMap().isEmpty()) {
                    ValidationMessage.getFromRootPage().showSuccessMessage("validation_stock_modify_warn");
                    return;
                }
                stockServiceAsync.saveStockModifications(stockTable.getStockModificationsMap(), new IabakoAsyncCallback<Void>() {
                    public void onSuccess(Void result) {
                        super.onSuccess(result);
                        NavigationController.getInstance().goTo(PageEnum.ProductSearch);
                        ValidationMessage.getFromRootPage().showSuccessMessage("validation_stock_modify_success");

                        AuthenticateUserProvider.getNotificationPanel().refresh(NotificationType.stockLowerThanLimit, NotificationType.stockRupture);
                        AuthenticateUserProvider.getTrackingPanel().refresh(TrackingType.stockModification);
                    }
                });
            }
        });
    }

    @Override
    protected DataTable getCustomDataTable() {
        stockTable = new StockTable(getSearchServiceAsync(), getQueryDefinitionDTO(), getSearchPageConfigEnum());
        return stockTable;
    }

    @Override
    protected HTMLPanel getContentPanel() {
        return binder.createAndBindUi(this);
    }

    @Override
    protected SearchServiceAsync getSearchServiceAsync() {
        return StockServiceAsync.Util.getInstance();
    }

    @Override
    protected CRUDServiceAsync getCRUDServiceAsync() {
        return null;
    }

    @Override
    protected SuggestServiceAsync getSuggestServiceAsync() {
        return ProductServiceAsync.Util.getInstance();
    }

    @Override
    protected HTMLPanel getResultsPanel() {
        return resultsPanel;
    }

    @Override
    protected HTMLPanel getSearchPanel() {
        return searchPanel;
    }

    @Override
    protected Composite getSearchForm(DataTable dataTable) {
        if (productSearchForm == null) {
            productSearchForm = new ProductSearchForm(new DefaultSuggestHandler(getSuggestServiceAsync()), dataTable);
        }
        return productSearchForm;
    }

    @Override
    protected PageConfigSearchEnum getSearchPageConfigEnum() {
        return PageConfigSearchEnum.StockManagement;
    }

    @Override
    protected PageEnum getModificationPage() {
        return null;
    }
}
